package com.example.spider.web;

import cn.hutool.core.lang.Tuple;
import com.example.spider.provider.multidownload.task.ParseDownloadFileTask;
import com.example.spider.task.SpiderTaskQueue;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.validation.constraints.NotEmpty;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 触发 http://localhost:8080/index.html
 * 触发 http://localhost:8080/download/parse
 */
@Validated
@RequestMapping("download")
@RestController
public class DownloadController {

    @RequestMapping(value = "parse")
    public String createNew(@NotEmpty String urlInfo) throws IOException {
        urlInfo = urlInfo.trim();
        ParseDownloadFileTask task = new ParseDownloadFileTask()
                .setId(UUID.randomUUID().toString());

        if (urlInfo.startsWith("http")) {
            // 直接下载
            throw new RuntimeException();
        } else if (urlInfo.startsWith("aria2c")) {
            List<Tuple> kvList = new LinkedList<>();
            String[] kvsArr = urlInfo.split("\" ");
            for (String kvs : kvsArr) {
                String[] kv = kvs.split(" \"", 2);
                kvList.add(new Tuple(kv[0], kv[1]));
            }
            String url = kvList.stream().filter(t -> t.get(0).equals("aria2c")).findFirst().orElseThrow().get(1);
            String fileName = kvList.stream().filter(t -> t.get(0).equals("--out")).findFirst().orElseThrow().get(1);
            Map<String, String> exHeaders = kvList.stream().filter(t -> t.get(0).equals("--header"))
                    .map(t -> (String) t.get(1))
                    .map(headerStr -> new Tuple((Object[]) headerStr.split(": ", 2)))
                    .collect(Collectors.toMap(t -> t.get(0), t -> t.get(1)));
            task.setUrl(url);
            task.setName(fileName);
            exHeaders.put("Range", "0-");
            exHeaders.put("Accept-Charset", "*");
            exHeaders.put("Accept-Encoding", "identity");
            task.getHeaders().putAll(exHeaders);
        } else if (urlInfo.startsWith("curl")) {

            throw new RuntimeException();
        }
//        else if(urlInfo.startsWith("bc://")) {
//
//        }

        SpiderTaskQueue.putTask(task);
        return "ok";
    }

    // aria2c "https://dl-pc-sz-cf.pds.quark.cn/6yGjSL7b/9408362/6437548984996e798d6b4c40bb27d1b8620fe70f/64375489cbeac8c074374f01bc17982a0447c5d2?Expires=1699196650&OSSAccessKeyId=LTAIyYfxTqY7YZsg&Signature=oOASA7QyQvov1X3TFzvpjNnCJ38%3D&x-oss-traffic-limit=503316480&response-content-disposition=attachment%3B%20filename%3DKoikatsu%2520Sunshine.7z.001%3Bfilename%2A%3Dutf-8%27%27Koikatsu%2520Sunshine.7z.001&callback-var=eyJ4OmF1IjoiLSIsIng6dWQiOiIxNi0wLTUtMi02LU4tNC1OLTEtMTYtMC1OIiwieDpzcCI6IjE5OSIsIng6dG9rZW4iOiI0LTliMWM5YmJiOTk4ZGU4YWVkNDg1NjA3MDhhODg0MmZlLTgtMS01MDAtODQwZWVhMTE4Y2JlNDQyYTkzYTY1MmQzYWY5ZGI2ZGUtMC0wLTAtMC1kN2ViZmUxOGIyYjE0ODU2OTgxM2VlNTc3MmVmZmI5MCIsIng6dHRsIjoiOTQzNzAwIn0%3D&callback=eyJjYWxsYmFja0JvZHlUeXBlIjoiYXBwbGljYXRpb24vanNvbiIsImNhbGxiYWNrU3RhZ2UiOiJiZWZvcmUtZXhlY3V0ZSIsImNhbGxiYWNrRmFpbHVyZUFjdGlvbiI6Imlnbm9yZSIsImNhbGxiYWNrVXJsIjoiaHR0cHM6Ly9jbG91ZC1hdXRoLmRyaXZlLnF1YXJrLmNuL291dGVyL29zcy9jaGVja3BsYXkiLCJjYWxsYmFja0JvZHkiOiJ7XCJob3N0XCI6JHtodHRwSGVhZGVyLmhvc3R9LFwic2l6ZVwiOiR7c2l6ZX0sXCJyYW5nZVwiOiR7aHR0cEhlYWRlci5yYW5nZX0sXCJyZWZlcmVyXCI6JHtodHRwSGVhZGVyLnJlZmVyZXJ9LFwiY29va2llXCI6JHtodHRwSGVhZGVyLmNvb2tpZX0sXCJtZXRob2RcIjoke2h0dHBIZWFkZXIubWV0aG9kfSxcImlwXCI6JHtjbGllbnRJcH0sXCJwb3J0XCI6JHtjbGllbnRQb3J0fSxcIm9iamVjdFwiOiR7b2JqZWN0fSxcInNwXCI6JHt4OnNwfSxcInVkXCI6JHt4OnVkfSxcInRva2VuXCI6JHt4OnRva2VufSxcImF1XCI6JHt4OmF1fSxcInR0bFwiOiR7eDp0dGx9LFwiZHRfc3BcIjoke3g6ZHRfc3B9LFwiaHNwXCI6JHt4OmhzcH0sXCJjbGllbnRfdG9rZW5cIjoke3F1ZXJ5U3RyaW5nLmNsaWVudF90b2tlbn19In0%3D&ud=16-0-5-2-6-N-4-N-1-16-0-N" --out "Koikatsu Sunshine.7z.001" --header "Cookie: b-user-id=b249dd41-4d43-917c-990e-b90bbb798c0d; __wpkreporterwid_=226e3f68-ac0f-40b9-3a71-2e171ea2cbe2; __itrace_wid=4d72fd5e-2bd6-4a91-0a92-3cde2ec2ea67; __kp=01f2c8c0-70f9-11ee-bc97-655eea42a749; __kps=AAQ6XnJUznxtztluJ4acvFcX; __ktd=M2Wko4GJREw4S/63u9Xp8w==; __uid=AAQ6XnJUznxtztluJ4acvFcX; ctoken=iz3NFLxkgV13sJeNzVslLydh; __puus=91448c65bb7756f9a7f5beea3b12724cAASc/bLRyzMNj5l/jCuArD55CoSWzyj5MTXFFJfPdQo08IZCqAjzkcTFlki3yzGVIXNSyxona0HpUe9Z+4xXjlu1JdU5Xm+tLSGxiyuLBUSv+F007snrSAKx2bHxrjvQ4Qmzz3T2C1TZWhIjbn3zaJ3DNACieJ41XRkYjYsT3xZdNDj8soEQetSs1vq9VuzvRezthgYmXPcptScqTP3QT9ml"

}
